// pages/mine/mine.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar, //模态动画
    CustomBar: app.globalData.CustomBar,
    tabbar:{},
    avatarUrl:"",
    nickName:"",
    check_identity:0
  },
  //模态动画
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.changeTabBar();
    wx.setNavigationBarTitle({
      title: '我的'
    })
    const avatarUrl = wx.getStorageSync('avatarUrl');
    const nickName = wx.getStorageSync('nickName');
    this.setData({
      avatarUrl: avatarUrl,
      nickName: nickName
    })
    var that=this
    const value = wx.getStorageSync('openid');
    app.util.request({
      url: 'admin/WxGoodsApi/check_identity',
      data:{
        uid: value
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          
          that.setData({
            check_identity: 1
          })
        }else if(res.data.code==1){
          that.setData({
            check_identity: 0
          })
        }

      },
      fail: function (res) {
        wx.showToast({
          title: '服务器响应失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  // 核销
  heXiao(){
    wx.scanCode({
      success(res) {
        console.log(res)
        var outrade_no = res.result;
        var that = this
        const value = wx.getStorageSync('openid');
        app.util.request({
          url: 'admin/WxGoodsApi/confirmOrder',
          data: {
            uid: value,
            order_no: outrade_no
          },
          method: 'post',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            console.log(res)
            if (res.data.code == 0) {

              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }

          },
          fail: function (res) {
            wx.showToast({
              title: '服务器响应失败',
              icon: 'none',
              duration: 2000
            })
          }
        });
      },
      complete(res) {

        console.log(res)
        wx.navigateBack({
          delta: 1,
        })
      }
    })
  },
  


  // 拨打客服电话
  dailService(e){
    var that=this
    that.showModal(e)
    wx.makePhoneCall({
      phoneNumber: '400-988-888', // 仅为示例，并非真实的电话号码
      success: "拨打成功",
      fail:"拨打失败",
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const value = wx.getStorageSync('openid');
    app.util.request({
      url: 'admin/WxGoodsApi/shopcarList',
      method: 'post',
      data: {
        uid: value
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var data = res.data.data;
        var num = 0;
        if (data === undefined || data.length == 0) {
          app.globalData.tabbar.list[2]['dott'] = num;
          app.setData();
        } else {
          for (var i = 0; i < data.length; i++) {
            num += data[i]['goods_num'];
          }
          app.globalData.tabbar.list[2]['dott'] = num;
          app.setData();
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '服务器响应失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})